<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
set_html('title','账单详情');
if($_RQ['op']=='main'){
	if($_RQ['id']){
		$id = intval($_RQ['id']);
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])));
	}elseif($_RQ['ordersn']){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>trim($_RQ['ordersn'])));
	}
	if(!$order){
		exi('指定订单不存在！','error','goback');
	}
	//时间格式化
	$order['createtime']=date('Y-m-d H:i:s',$order['createtime']);
	$order['paytime']=date('Y-m-d H:i:s',$order['paytime']);
	$order['sendtime']=$order['express']['createtime']?date('Y-m-d H:i:s',$order['express']['createtime']):'';
	$order['finishtime']=date('Y-m-d H:i:s',$order['finishtime']);
	if ($order['userdeleted'] == 2) {
		exi('订单已经被删除!','error');
	}
	//用户地址
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && $order['address']['name']){
		$address=$order['address'];
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	$address['pcd']=json_decode($address['pcd'],true);
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));                       //用户信息
    $ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
	$merchs =array();
	foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','unit','type','image','title','merchid'));
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			if($_SESSION['is_tomedia']){
				$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
			}
			$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
			$gv['title']=$goodsinfo['title'];
			$gv['image']=$goodsinfo['image'];
			$gv['merchid']=$goodsinfo['merchid'];	
			//获取门店
			$stores = array();
			$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
			if ($goodsinfo['isverify'] == 1) {
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
					}
				}
			}elseif($shopset['openstore'] ==1){
				if (empty($goodsinfo['storeids'])) {
					$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
				}else {
					foreach($goodsinfo['storeids'] as $sv){
						$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
					}
				}
			}
			$goodsinfo['store']=$stores;
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			$goodsinfo['buytypetitle']=$this->buytypes()[$order['buytype']];
			//获取商户
			if($goodsinfo['merchid']){
				$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$merchs[$goodsinfo['merchid']] = $merchinfo;
				$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
				$return['ismerch']=1;
			}else{
				$merchs[0]['goods'][] = $goodsinfo;
			}
			if($order['storeid']){
				$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$order['storeid']));
			}
	}	
	$return['self']=pdo_get('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
	$return['merchs']=$merchs;
	if($order['api_data'] && $order['api_pluginname']=='jy_shop1miao'){
		$order['api_data']=json_decode($order['api_data'],true);
		$order['is_zu']=1;
	}
	$allqi=$order['api_data']['allqi'];
	$zudan=array();
	$starttime=strtotime($order['api_data']['starttime']);
	for($i=1;$i<=$allqi;$i++){
		$day=$starttime+30*($i-1)*24*3600;
		$day=date('Y-m-d',$day);
		$log=$this->pdo_get('log',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id'],'qinum'=>$i));
		$ispay=$log['ispay'];
		if($ispay){
			$desc='该期租金已全额支付！';
		}else{
			$desc='系统将于'.$day.'前往支付宝发起扣款，请确保余额充足以免产生罚金！';
		}
		$zudan[]=array(
			'price'=>$order['api_data']['total_price'],
			'index'=>$i,
			'desc'=>$desc,
			'ispay'=>$ispay,
			'remark'=>'第'.$i.'期租金',
		);
	}
	$order['zudan']=$zudan;
	$return['order']=$order;
	$return['member']=$member;
	$return['goods']=$ordergoods;
	$return['stores']=$stores;
	$return['order_data']=json_decode($order_data,true);
	$return['post']=array('libaos'=>'');
	exi($return);
}
//支付订单
if($_RQ['op']=='onepay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$order['api_data']=json_decode($order['api_data'],true);
	$price=$order['api_data']['total_price'];
	$log=$this->model('model')->uplog($order,$_RQ['qinum']);
	$logid=$log['id'];
	$payid=$log['pay_id'];
	$res=array(
		'order_id'=>$logid,
		'pay_id'=>$payid,
	);
	exi($res);
}
//支付订单
if($_RQ['op']=='allpay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$id));
	$order['api_data']=json_decode($order['api_data'],true);
	$log=$this->pdo_get('log',array('orderid'=>$id,'qinum'=>0));
	$logid=$log['id'];
	$payid=$log['pay_id'];
	if(!$logid){
		$price=0;
		$allqi=$order['api_data']['allqi'];
		$zudan=array();
		for($i=1;$i<=$allqi;$i++){
			$log=$this->model('model')->uplog($order,$i,true);
			if(!$log['ispay']){
				$price +=$log['price'];
			}
		}
		$log=array(
			'uniacid'=>$_SESSION['uniacid'],
			'orderid'=>$id,
			'mid'=>$_SESSION['uid'],
			'price'=>$price,
			'payfee'=>$price,
			'qinum'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP
		);
		$this->pdo_insert('log',$log);
		$logid=pdo_insertid();
		$params=array(
			'id'=>$logid,
			'title'=>'全部续租',
			'fee'=>$price,
			'payfee'=>$price,
			'back_url'=>'jy_shop1miao/order.zudan/payresult',
			'success_url'=>'jy_shop1miao/order.list/',
			'error_url'=>'jy_shop1miao/order.list/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('log',array('pay_id'=>$payid),array('id'=>$logid));
	}
	$res=array(
		'order_id'=>$logid,
		'pay_id'=>$payid,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error');
	}
	$order=$this->pdo_get('log',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error');
	}
	if($order['payfee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	if($order['qinum']){
		$this->pdo_update('log',array('ispay'=>1,'status'=>1,'paytime'=>TIMESTAMP,'paytype'=>$payresult['pay_type']),array('id'=>$order['id']));
	}else{
		$this->pdo_update('log',array('ispay'=>1,'status'=>1,'paytime'=>TIMESTAMP,'paytype'=>$payresult['pay_type']),array('orderid'=>$order['orderid']));
	}
	exi('提交成功!');
}
?>